import axios from 'axios'
import { ElMessage, ElMessageBox } from 'element-plus';

// import { ElMessage, ElMessageBox } from 'element-plus';
import { Session } from '@/utils/storage';

const service = axios.create({
    baseURL: import.meta.env.VITE_API_URL as any,
    timeout: 5000,
	headers: { 'Content-Type': 'application/json' },

})

// 请求拦截器
service.interceptors.request.use(
    config => {
        // 添加请求头
        if (Session.get('token')) {
			(<any>config.headers).common['Authorization'] = `Bearer ${Session.get('token')}`;
		}
        return config
    },
    error => {
        return Promise.reject(error)
    }
)

// 响应拦截器
service.interceptors.response.use(
    response => {
        	// 对响应数据做点什么
		const res = response.data;
		const code = response.data.code;

        if (code === 401) {
			ElMessageBox.alert('登录状态已过期，请重新登录', '提示', {confirmButtonText:'确定'})
				.then(() => {
					Session.clear(); // 清除浏览器全部临时缓存
					window.location.href = '/'; // 去登录页
				})
				.catch(() => {});
		} else if (code !== 0) {
			ElMessage.error(res.message)
			return Promise.reject(new Error(res.message))
		} else {
			return res
		}

    },
    error => {
        if (error.message.indexOf('timeout') != -1) {
			ElMessage.error('网络超时');
		} else if (error.message == 'Network Error') {
			ElMessage.error('网络连接错误');
		} else {
			if (error.response.data) ElMessage.error(error.response.statusText);
			else ElMessage.error('接口路径找不到');
		}
		return Promise.reject(error);
    }

)
export default service
